﻿using System;
using System.Collections.Generic;
using _2DGame_Preview_Server.Services.RoomAndBattle.RoomHall.Model;
using Assets.Script.NetSystem.Services.HallService;
using SimpleTools.Net.SimpleServer.UDP;
using UnityEngine;

namespace Script.UiSystem.Hall
{

    public class HallController
    {
        public static HallController Instance => Nested.instance;

        public HallClient HallClient { get; set; }
        
        
        class Nested
        {
            static Nested()
            {
            }
            internal static readonly HallController instance = new HallController();
        }

        public HallView HallView {
            get;
            set;
        }
        
        public HallController()
        {
            GameObject go = new GameObject("HallManager");
            HallView = go.AddComponent<HallView>();
        }

        public void InitRooms(List<RoomInfo> rooms)
        {
            HallView.Init(rooms);
        }

        public void UpdateRooms(int roomId, int current)
        {
            HallView.UpdateRooms(roomId, current);
        }

        public void DelRoom(int roomId)
        {
            HallView.DelRoom(roomId);
        }

        public void NewRoom(RoomInfo roomInfo)
        {
            HallView.NewRoom(roomInfo);
        }
        
    }
    
    public class HallView : MonoBehaviour
    {

        [SerializeField] private GameObject roomModel;
        private Transform roomTransformParent;

        private List<RoomModel> models;

        private void Awake()
        {
            roomModel = Resources.Load<GameObject>("Hall&Room/RoomCard");
            
            try
            {
                roomTransformParent = GameObject.Find("RoomContent").GetComponent<Transform>();
            }
            catch (Exception e)
            {
                Debug.LogError("could not find RoomContent. Please create it to finish initializing");
                throw;
            }

            models = new List<RoomModel>();
        }

        public void Init(List<RoomInfo> rooms)
        {
            if (roomTransformParent == null)
            {
                roomTransformParent = GameObject.Find("RoomContent").GetComponent<Transform>();
            }
            
            foreach (var room in rooms)
            {
                GameObject go = Instantiate(roomModel, roomTransformParent, true);
                RoomModel model = go.GetComponent<RoomModel>();
                model.InitView(room);
                models.Add(model);
            }
        }
        
        
        public void UpdateRooms(int roomId, int current)
        {
            foreach (var room in models)
            {
                if (room.RoomId == roomId)
                {
                    room.UpdateCap(current);
                }
            }   
        }

        public void DelRoom(int roomId)
        {
            for (int i = 0; i < models.Count; i++)
            {
                if (models[i].RoomId != roomId) continue;
                
                Destroy(models[i].gameObject);
                break;
            }
        }

        public void NewRoom(RoomInfo roomInfo)
        {
            GameObject go = Instantiate(roomModel, roomTransformParent, true);
            RoomModel model = go.GetComponent<RoomModel>();
            model.InitView(roomInfo);
            models.Add(model);
        }
    }

}